﻿using KingApp.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KingApp.DB;
using KingApp.Common;
using KingApp.Model;


namespace KingApp
{
    public partial class Main : Form
    {
        private List<CheckBox> ChkDans = new List<CheckBox>();
        private List<CheckBox> ChkKillers = new List<CheckBox>();
        private List<CheckBox> ChkSums = new List<CheckBox>();

        public Main()
        {
            InitializeComponent();
            this.ChkComDansEventBinding();
            this.ChkComKillersEventBinding();

            foreach (var sum in this.gbComSum.Controls)
            {
                if (sum is CheckBox)
                {
                    CheckBox s = sum as CheckBox;
                    s.CheckState = CheckState.Checked;
                    if (s.Tag == "sumChk")
                        this.ChkSums.Add(s);
                }
            }
        }

        private void ChkComDansEventBinding()
        {
            foreach (var c in this.gbComDan.Controls)
            {
                if (c is CheckBox)
                {
                    CheckBox chk = c as CheckBox;
                    chk.Click += new EventHandler(ChkComDan_Click);
                    this.ChkDans.Add(c as CheckBox);
                }
            }

        }

        private void ChkComKillersEventBinding()
        {
            foreach (var c in this.gbComKiller.Controls)
            {
                if (c is CheckBox)
                {
                    CheckBox chk = c as CheckBox;
                    chk.Click += new EventHandler(ChkComKiller_Click);
                    this.ChkKillers.Add(c as CheckBox);
                }
            }

        }

        private void ChkComDan_Click(object sender, EventArgs e)
        {
            CheckBox sc = sender as CheckBox;
            this.txtComDan.Text = string.Empty;
            foreach (var chk in this.ChkDans)
            {
                if (chk.CheckState == CheckState.Checked)
                {
                    if (this.txtComDan.Text == string.Empty)
                    {
                        this.txtComDan.Text += chk.Text;
                    }
                    else
                    {
                        this.txtComDan.Text += "," + chk.Text;
                    }
                }
            }

            if (!string.IsNullOrEmpty(this.txtComDan.Text))
            {
                this.ChkDans.Where(c => c.CheckState == CheckState.Unchecked).Select(c => c.Enabled = false).ToList();
            }
            else
            {
                this.ChkDans.Where(c => c.CheckState == CheckState.Unchecked && !this.txtComKiller.Text.Split(',').Contains(c.Text)).Select(c => c.Enabled = true).ToList();
            }


            this.ChkKillers.Where(c => c.Text == sc.Text && sc.CheckState == CheckState.Checked).Select(c => c.Enabled = false).ToList();
            this.ChkKillers.Where(c => c.Text == sc.Text && sc.CheckState == CheckState.Unchecked && this.txtComKiller.Text.IndexOf(",") == -1).Select(c => c.Enabled = true).ToList();
        }

        private void ChkComKiller_Click(object sender, EventArgs e)
        {
            CheckBox sc = sender as CheckBox;
            this.txtComKiller.Text = string.Empty;
            foreach (var chk in this.ChkKillers)
            {
                if (chk.CheckState == CheckState.Checked)
                {
                    if (this.txtComKiller.Text == string.Empty)
                    {
                        this.txtComKiller.Text += chk.Text;
                    }
                    else
                    {
                        this.txtComKiller.Text += "," + chk.Text;
                    }
                }
            }

            if (this.txtComKiller.Text.Split(',').Length == 2)
            {
                this.ChkKillers.Where(c => c.CheckState == CheckState.Unchecked).Select(c => c.Enabled = false).ToList();
            }
            else
            {
                this.ChkKillers.Where(c => c.CheckState == CheckState.Unchecked && c.Text != this.txtComDan.Text).Select(c => c.Enabled = true).ToList();
            }

            this.ChkDans.Where(c => c.Text == sc.Text && sc.CheckState == CheckState.Checked).Select(c => c.Enabled = false).ToList();
            this.ChkDans.Where(c => c.Text == sc.Text && sc.CheckState == CheckState.Unchecked && string.IsNullOrEmpty(this.txtComDan.Text)).Select(c => c.Enabled = true).ToList();
        }

        private void EnableCheckBoxs(IEnumerable<CheckBox> chks, bool enabled = true)
        {
            chks.Where(c => c.CheckState == CheckState.Unchecked).Select(c => c.Enabled = enabled).ToList();
        }

        private void BindData()
        {

            this.bsource.DataSource = LotteryData.GetInstance().Lotterys.Where(l => this.dateTimePicker1.Value.ToShortDateString() == l.Time);
            this.bnav.BindingSource = this.bsource;
            this.dgLottery.AutoGenerateColumns = false;
            this.dgLottery.Columns[0].DataPropertyName = "Id";
            this.dgLottery.Columns[0].Name = "Id";
            this.dgLottery.Columns[0].HeaderText = "ID";
            this.dgLottery.Columns[0].Width = 40;
            this.dgLottery.Columns[1].DataPropertyName = "Issue";
            this.dgLottery.Columns[1].Name = "Issue";
            this.dgLottery.Columns[1].HeaderText = "期号";
            this.dgLottery.Columns[2].DataPropertyName = "Time";
            this.dgLottery.Columns[2].Name = "Time";
            this.dgLottery.Columns[2].HeaderText = "开奖时间";
            this.dgLottery.Columns[3].DataPropertyName = "LotteryNumber";
            this.dgLottery.Columns[3].Name = "LotteryNumber";
            this.dgLottery.Columns[3].HeaderText = "开奖号码";

            this.dgLottery.Columns[4].DataPropertyName = "Sum";
            this.dgLottery.Columns[4].Width = 40;
            this.dgLottery.Columns[4].Name = "Sum";
            this.dgLottery.Columns[4].HeaderText = "和值";

            this.dgLottery.Columns[5].DataPropertyName = "SumTail";
            this.dgLottery.Columns[5].Width = 40;
            this.dgLottery.Columns[5].Name = "SumTail";
            this.dgLottery.Columns[5].HeaderText = "合值";

            this.dgLottery.Columns[6].DataPropertyName = "MidCode";
            this.dgLottery.Columns[6].Width = 40;
            this.dgLottery.Columns[6].Name = "MidCode";
            this.dgLottery.Columns[6].HeaderText = "中数";

            this.dgLottery.Columns[7].DataPropertyName = "Lottery012";
            this.dgLottery.Columns[7].Name = "Lottery012";
            this.dgLottery.Columns[7].HeaderText = "012";

            this.dgLottery.Columns[8].DataPropertyName = "Lottery0123";
            this.dgLottery.Columns[8].Name = "Lottery0123";
            this.dgLottery.Columns[8].HeaderText = "0123";

            this.dgLottery.Columns[9].DataPropertyName = "Lottery01234";
            this.dgLottery.Columns[9].Name = "Lottery01234";
            this.dgLottery.Columns[9].HeaderText = "01234";

            this.dgLottery.Columns[10].DataPropertyName = "OddCode";
            this.dgLottery.Columns[10].Name = "OddCode";
            this.dgLottery.Columns[10].HeaderText = "OddCode";


            this.dgLottery.Columns[11].DataPropertyName = "OddEven";
            this.dgLottery.Columns[11].Name = "OddEven";
            this.dgLottery.Columns[11].HeaderText = "OddEven";
            this.dgLottery.DataSource = this.bsource;

        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            TimeSpan ts = DateTime.Now - this.dateTimePicker1.Value;
            LotteryData.ClearInstance();
            XmlHelper.UpdateLotteryData(ts.Days);


            //this.lblCount.Text = (LotteryData.GetInstance().Lotterys.Sum(l => l.CorrectCount) * 3.8 - LotteryData.GetInstance().Lotterys.Count() * 8).ToString();

            // this.lblCount.Text = LotteryData.GetInstance().Lotterys.Sum(l => l.CorrectCount).ToString();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            var Combinations = CombinationHelper.GetCombinations(5, 11).ToList();
            int index = 1;
            this.dgResult.AutoGenerateColumns = false;

            this.dgResult.DataSource = SourceData.GetInstance()
                .Lotterys.Where(l => l.CodeThrid != 3 && l.CodeThrid != 9 && l.OddEven == "2:3" && !l.HasCode(new int[] { 3, 7 }) && l.HasCode(new int[] { 2, 8 }))
                .Select(l => new { Id = index++, LotteryNumber = l.LotteryNumber }).ToList();
            this.dgResult.Columns[0].DataPropertyName = "Id";
            this.dgResult.Columns[1].DataPropertyName = "LotteryNumber";

        }

        private void btnComSumbit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtComDan.Text))
            {
                MessageBox.Show("请选择胆码！");
                return;
            }

            if (string.IsNullOrEmpty(this.txtComKiller.Text))
            {
                MessageBox.Show("请选择杀码！");
                return;
            }

            int dan = int.Parse(this.txtComDan.Text);
            int[] killCodes = this.txtComKiller.Text.Split(',').Select(c => int.Parse(c)).ToArray();
            List<string> oddEvens = new List<string>();
            foreach (var c in this.gbComOddEven.Controls)
            {
                if (c is CheckBox)
                {
                    CheckBox chk = c as CheckBox;
                    if (chk.CheckState == CheckState.Checked)
                    {
                        oddEvens.Add(chk.Text);
                    }
                }
            }

            //codes.Add(dan);
            //for (int i = 1; i < 12; i++)
            //{
            //    if (dan != i && !killCodes.Contains(i))
            //        codes.Add(i);
            //}
            var sums = this.ChkSums.Where(s => s.CheckState == CheckState.Checked).Select(s => int.Parse(s.Text)).ToList();
            var lotterys = SourceData.GetInstance().Lotterys
                .Where(l => oddEvens.Contains(l.OddEven)
                    && l.HasCodeCount(new int[] { 1, 2, 3, 4 }) >= 1
                    && l.HasCodeCount(new int[] { 1, 2, 3, 4 }) < 3
                    && l.HasCodeCount(killCodes) < 1
                    && l.HasCode(new int[] { dan })
                    && sums.Contains(l.Sum));

            this.dgOneKiller.AutoGenerateColumns = false;
            int index = 1;
            this.dgOneKiller.DataSource = lotterys.Select(l => new Lottery { Id = index++, LotteryNumber = l.LotteryNumber }).ToList();//CombinationHelper.GetComsFiveFiveOfNine(codes).Select(l => new Lottery { Id = index++, LotteryNumber = string.Join(",", l) }).ToList();

            this.dgOneKiller.Columns[1].DataPropertyName = "LotteryNumber";
            this.dgOneKiller.Columns[0].DataPropertyName = "Id";
        }

        private void cheSumAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox sc = sender as CheckBox;
            if (sc.CheckState == CheckState.Checked)
            {
                this.ChkSums.Select(s => s.CheckState = CheckState.Checked).ToList();
            }
            else
            {
                this.ChkSums.Select(s => s.CheckState = CheckState.Unchecked).ToList();
            }
        }

        private void chkSumOdd_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox sc = sender as CheckBox;
            if (sc.CheckState == CheckState.Checked)
            {
                this.ChkSums.Where(s => int.Parse(s.Text) % 2 == 1).Select(s => s.CheckState = CheckState.Checked).ToList();
            }
            else
            {
                this.ChkSums.Where(s => int.Parse(s.Text) % 2 == 1).Select(s => s.CheckState = CheckState.Unchecked).ToList();
            }
        }

        private void chkSumEven_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox sc = sender as CheckBox;
            if (sc.CheckState == CheckState.Checked)
            {
                this.ChkSums.Where(s => int.Parse(s.Text) % 2 == 0).Select(s => s.CheckState = CheckState.Checked).ToList();
            }
            else
            {
                this.ChkSums.Where(s => int.Parse(s.Text) % 2 == 0).Select(s => s.CheckState = CheckState.Unchecked).ToList();
            }
        }

        private void chkSumLarge_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox sc = sender as CheckBox;
            if (sc.CheckState == CheckState.Checked)
            {
                this.ChkSums.Where(s => int.Parse(s.Text) > 35).Select(s => s.CheckState = CheckState.Checked).ToList();
            }
            else
            {
                this.ChkSums.Where(s => int.Parse(s.Text) > 35).Select(s => s.CheckState = CheckState.Unchecked).ToList();
            }
        }

        private void chkSumMiddle_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox sc = sender as CheckBox;
            if (sc.CheckState == CheckState.Checked)
            {
                this.ChkSums.Where(s => int.Parse(s.Text) < 36 && int.Parse(s.Text) > 24).Select(s => s.CheckState = CheckState.Checked).ToList();
            }
            else
            {
                this.ChkSums.Where(s => int.Parse(s.Text) < 36 && int.Parse(s.Text) > 24).Select(s => s.CheckState = CheckState.Unchecked).ToList();
            }
        }

        private void chkSumSmall_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox sc = sender as CheckBox;
            if (sc.CheckState == CheckState.Checked)
            {
                this.ChkSums.Where(s => int.Parse(s.Text) < 25).Select(s => s.CheckState = CheckState.Checked).ToList();
            }
            else
            {
                this.ChkSums.Where(s => int.Parse(s.Text) < 25).Select(s => s.CheckState = CheckState.Unchecked).ToList();
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            this.BindData();
        }


    }


}
